 
#!/bin/bash
#
#   install script - tbn-agent linux version
#   authors - TBN team:
#   Mike D'Morto ( MikeDM ) <MikeDMorto@gmail.com>
#   gemozlobin <gemozlobin@gmail.com>
#
#   27.12.2009
#

echo "Welcome to install tbn-agent for linux"
echo "press Enter for install or Ctrl + C for abort" 
read
ROOT_UID=0
if [ "$UID" -eq "$ROOT_UID" ]  
then
  echo "root access is success";
else
  echo "sorry - need root access";
  exit 1;
fi


#test curl
echo -n "test curl ...";
test -x $(whereis -b curl | cut -f 2 -d " ")
ret=$?
if [[ $ret != '0' ]]
then echo "... error"; echo "fix problem - install curl"; exit 1;
fi

echo -n "make dirs ... "
mkdir -p /var/lib/tbn 2>/dev/null
ret=$?
if [[ $ret != '0' ]]
then echo "failure create /var/lib/tbn"; exit 1;
fi

mkdir -p /etc/tbn 2>/dev/null
ret=$?
if [[ $ret != '0' ]]
then echo "failure create /etc/tbn"; exit 1;
fi

mkdir -p /var/log/tbn 2>/dev/null
ret=$?
if [[ $ret != '0' ]]
then echo "failure create /var/log/tbn"; exit 1;
fi
echo "success"

echo -n "copy tbn-agent script ... "
cp ./tbn-agent.sh /sbin/tbn-agent.sh
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi
echo "success"

echo -n "create config file ... "
echo "#config file for TBN-agent" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi

echo "getter=\"$(whereis -b curl | cut -f 2 -d " ")\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi


echo "db=\"/var/lib/tbn\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi


echo "flog=\"/var/log/tbn/tbn.log\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi
echo "dirlog=\"/var/log/tbn\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi
echo "ftmp=\"/tmp/tbn.tmp\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi
echo "flock=\"/var/run/tbn.lock\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi
echo "success"

echo -n "setup permissions ... "
chown root:root -R /etc/tbn && chown root:root /sbin/tbn-agent.sh && chown root:root /var/lib/tbn && chown root:root /var/log/tbn/
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi
chmod 600 -R /etc/tbn && chmod 500 /sbin/tbn-agent.sh && chmod 700 /var/lib/tbn && chmod 700 /var/log/tbn/
ret=$?
if [[ $ret != '0' ]]
then echo "failure"; exit 1;
fi
echo "success"

#set proto http or https

echo "input server protocol [http or https] (default https)"
read proto;
case "$proto" in
    http)
	;;
    https)
	;;
    *)
	echo "protocol not correct - set as https";
	$proto="https";
	;;
esac

echo "proto=\"$proto\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "Error write data to config file"; exit 1;
fi

echo "input server address [for example \"myserver.com\"]"
read server;
echo "server=\"$server\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "Error write data to config file"; exit 1;
fi

echo "input path to tbn-server.php [for example \"/tbn/tbn-server.php\"]"
read s_path;
echo "s_path=\"$s_path\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "Error write data to config file"; exit 1;
fi

echo "input login for server [for example \"mylogin\"]"
read login
echo "login=\"$login\";"  >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "Error write data to config file"; exit 1;
fi
echo "input password for server [for example \"mypass\"]"
read pass
echo "pass=\"$pass\";"  >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "Error write data to config file"; exit 1;
fi

echo "input server key [for example \"SERVER-KEY-122211334455kldsaf-00\"]"
read serverkey;
echo "serverkey=\"$serverkey\";" >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "Error write data to config file"; exit 1;
fi

echo "input bot name [for example \"Bot\"]"
read name
echo "name=\"$name\";"  >> /etc/tbn/tbn-agent.cfg
ret=$?
if [[ $ret != '0' ]]
then echo "Error write data to config file"; exit 1;
fi

echo "if used https protocol:"
echo "copy your ssl cert of server"
echo "to /etc/tbn-agent directory"
echo "and name cert = server.crt"
echo "else cert not correct or other errors"
echo "use hack -> set tbn-agent.cfg ignoressl=\"yes\""
echo "alert: hack is unsafe https" 
echo "-------------------------------"
echo "hacks:"
echo "ignorecache - ignore cache dir = yes or no"
echo "ignoressl - ignore ssl cert = yes or no"
echo "-------------------------------"
echo "write data to config is success"
echo "for start tbn-agent need append him to crontab for user ROOT"
echo "example:"
echo "crontab -e"
echo "and insert string"
echo "*/5 * * * * /sbin/tbn-agent.sh"
echo "good luck dude ^^"
